from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 创建 Chrome 浏览器实例
driver = webdriver.Edge()

try:
    # 打开百度首页
    driver.get('https://www.baidu.com')

    # 找到搜索框元素并输入关键词
    search_box = driver.find_element(By.ID, 'kw')
    search_box.send_keys('Python Selenium')

    # 找到搜索按钮并点击
    search_button = driver.find_element(By.ID, 'su')
    search_button.click()

    # 等待页面加载
    time.sleep(5)

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # 关闭浏览器
    driver.quit()
